package controllers;

import java.util.List;

import rbac.models.Oplog;
import rbac.models.User;

import com.et.mvc.JsonView;
import com.et.mvc.View;

import utils.Md5Util;

import models.Info;

import models.Banner;
import models.Estate2;
import models.Hotkeyword;
import models.Infotype;
import models.Reply;
import models.Zjlist;

import models.Link;



public class PController extends PortalController {
	public void index() throws Exception {
	  // 
	}
	
	public View postReply(String local_id,String name,String email,String phone,String  message) throws Exception {
		Reply item = new Reply();
		item.setLocal_id(local_id);
		item.setClientemail(email);
		item.setClientmsg(message);
		item.setClientname(name);
		item.setClientphone(phone);
		item.setPassflag(-1);
		item.setScore(5);
		item.setToweb(0);
		item.setReplydate(new java.sql.Timestamp(System
				.currentTimeMillis()));
		item.save();
		return new JsonView("success:true");
	}
	
	public void search() throws Exception{
		//生成查询语句
		String term = "";
		term = Md5Util.getBASE64(term);
		redirect("/p/list?term=" + term);
	}
	
	public void list(String cd,int page, int rows,String	term) throws Exception {
		   if (rows == 0) rows = 30;
		   if (page == 0) page = 1;
		   String cond = "1=1";
		   if ((term != null) && (!term.equals("")))
		   {			   
			   cond = Md5Util.getFromBASE64(term);
			   request.setAttribute("titlename", "条件查询");
			   request.setAttribute("term", term);
		   }else{
		     Hotkeyword item = Hotkeyword.findFirst(Hotkeyword.class," code = ?",new Object[]{ cd });
		     if (item == null)
			   redirect("/p/notfind");
		     else{
			   request.setAttribute("titlename", item.getWord());
			   cond = " flag  = '0' and " + item.getCond();
		     }
		   }
		   
		   long total = Estate2.count(Estate2.class, cond, null);
		   List<Estate2> items = Estate2.findAll(Estate2.class, cond, null, "editdate desc", rows, (page-1)*rows);			   
		   request.setAttribute("total", total);
		   request.setAttribute("pageno", page);
		   request.setAttribute("cd", cd);
		   request.setAttribute("esinfos", items);

	}
	
	public void partlist(int typeid,int page, int rows) throws Exception {
		   if (rows == 0) rows = 30;
		   if (page == 0) page = 1;
		   Infotype item = Infotype.findFirst(Infotype.class," id = ?",new Object[]{ typeid });
		   if (item == null)
			   redirect("/p/notfind");
		   else{
			   request.setAttribute("titlename", item.getName());
			   String cond = " passflag  = 0 and infotypeid = ?" ;
			   long total = Info.count(Info.class, cond, new Object[]{typeid});
			   List<Info> items = Info.findAll(Info.class, cond, new Object[]{typeid}, "totop desc,editdate desc", rows, (page-1)*rows);			   
			   request.setAttribute("total", total);
			   request.setAttribute("pageno", page);
			   request.setAttribute("typeid", typeid);
			   request.setAttribute("esinfos", items);
		   }
	}
	
	public void partdetail(int id) throws Exception {
		   Info item = Info.findFirst(Info.class,"id = ? and passflag = 0 ",new Object[]{ id } ,"editdate desc");
		   if (item == null)
			   redirect("/p/notfind");
		   else{
			   item.setHot(item.getHot()+1);
			   item.save();
			   item.setContent(Md5Util.getFromBASE64(item.getContent()));
			   request.setAttribute("titlename", item.getTitle());
			   request.setAttribute("esinfo", item);
		   }
	}
	
	public void detail(String id) throws Exception {
		   Estate2 item = Estate2.findFirst(Estate2.class,"local_id = ? and flag = '0' and (display = 0 or display = 3) ",new Object[]{ id } ,"editdate desc");
		   if (item == null)
			   redirect("/p/notfind");
		   else{
			   Zjlist zjlist = Zjlist.findFirst(Zjlist.class,"Company_ID = ? " , new Object[]{ item.getZ_id() });
			   request.setAttribute("showcaption", "show");
			   request.setAttribute("titlename", item.getAddress());
			   request.setAttribute("esinfo", item);
			   request.setAttribute("zjlist", zjlist);
		   }
	}
	
	public void notfind() throws Exception {
		
	}
	
	public void links() throws Exception {
		List<Link> links = Link.findAll(Link.class," totop = 1 ",null,"totop desc,sort");
		request.setAttribute("links", links);
	}
	
}
